home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / x2ftp / msdos / source / demostu2 / fileos1.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1994-12-08  |  5.6 KB  |  252 lines

  1. UNIT FILEOS1;
  2. {
  3.   Big Virtual File Operating System (better than OS/2)
  4.   - by Bjarke Viksoe, Aug 1994
  5.   Compiles multiple datafiles into one big file of crap.
  6.   Ups, files cannot be larger than 65535 bytes! Will fix that l8r.
  7.  
  8.   How to create one:
  9.      Make a textfile (with ext .lst) with all filename entries.
  10.      Create this Pascal program:
  11.         program make; uses fileos1; begin CreateBigFile(ParamStr(1)); end.
  12.      Compile it.
  13.      Run it with the name of your textfile (without .lst ext) as parameter!
  14.      The new compiled file has same name, but a .dat ext.
  15.   How to use it:
  16.      Include the unit FILEOS1 in your program.
  17.      Run InitFileSystem with name of the compiled file.
  18.      Get files with "GetFile" procedure (include file ext, but no path).
  19.      Remember to close it all with CloseFileSystem before ending program.
  20. }
  21.  
  22. INTERFACE
  23.  
  24. USES
  25.     DOS;
  26.  
  27. TYPE
  28.     string12 = string[12];
  29.  
  30.  
  31. function CreateBigFile(name : string) : boolean;
  32.  
  33. function InitFileSystem(name : string) : boolean;
  34. function GetFile(name : string12; buffer : pointer) : boolean;
  35. function GetFileSize(name : string12) : longint;
  36. procedure CloseFileSystem;
  37.  
  38.  
  39. (*=--------------------------------------------=*)
  40.  
  41.  
  42. IMPLEMENTATION
  43.  
  44. CONST
  45.     MAXFILES = 100;
  46.  
  47. TYPE
  48.     filerec = RECORD
  49.         name : string12;
  50.         pos,size : longint;
  51.     end;
  52.  
  53.     pDirectoryBuffer = ^DirectoryBuffer;
  54.     DirectoryBuffer = RECORD
  55.         entries : integer;
  56.         entry : array[1..MAXFILES] of filerec
  57.     end;
  58.  
  59.  
  60. VAR
  61.     {this is the datafile handle}
  62.     F : File;
  63.     {here is our directory structure.
  64.      And nope, we don't reserve MAXFILES records, but allocate dynamically!}
  65.     Directory : pDirectoryBuffer;
  66.  
  67.  
  68. (*=--------------------------------------------=*)
  69.  
  70. function CreateBigFile(name : string) : boolean;
  71. var
  72.     dir : pDirectoryBuffer;
  73.     ScriptFile : text;
  74.     TempFile : file;
  75.     pos,size : longint;
  76.     mem : pointer;
  77.     P: PathStr;
  78.     D: DirStr;
  79.     N: NameStr;
  80.     E: ExtStr;
  81. const
  82.     copyright : string[17] = '(c) Bjarke Viksoe';
  83. begin
  84. {$I-}
  85.     CreateBigFile:=FALSE;
  86.  
  87.     Assign(ScriptFile, name+'.lst');
  88.     Reset(ScriptFile);
  89.     if (IOresult<>0) then begin
  90.         Writeln('ERROR CREATING BIG-FILE: ',name,'.lst not found.');
  91.         exit;
  92.     end;
  93.     Assign(F, name+'.dat');
  94.     Rewrite(F,1);
  95.     if (IOresult<>0) then begin
  96.         Writeln('ERROR CREATING BIG-FILE: Cannot create ',name,'.dat ?');
  97.         Close(ScriptFile);
  98.         exit;
  99.     end;
  100.  
  101.     {Write 'blank' number entries and directory pos}
  102.     BlockWrite(F,pos,2);
  103.     BlockWrite(F,pos,4);
  104.     BlockWrite(F,copyright[1],length(copyright));
  105.  
  106.     New(dir);
  107.     GetMem(mem,65535);
  108.  
  109.     dir^.entries:=1;
  110.     while not EOF(ScriptFile) do begin
  111.         {find next filename entry}
  112.         ReadLn(ScriptFile,P);
  113.         if (P='') OR (P[1]=';') then continue;
  114.  
  115.         {open file to append...}
  116.         Assign(tempFile,P);
  117.         Reset(tempFile,1);
  118.         if (IOresult<>0) then begin
  119.             Writeln('ERROR CREATING BIG-FILE: File not found: ',P);
  120.             Dispose(dir);
  121.             FreeMem(mem,65535); Close(ScriptFile); Close(F);
  122.             exit;
  123.         end;
  124.  
  125.         FSplit(P,D,N,E);
  126.         {update directory list}
  127.         dir^.entry[dir^.entries].name:=N+E;
  128.         dir^.entry[dir^.entries].pos:=FilePos(F);
  129.         size:=FileSize(tempFile);
  130.         dir^.entry[dir^.entries].size:=size;
  131.  
  132.         {append file to big file}
  133.         BlockRead(tempFile,mem^,size);
  134.         BlockWrite(F,mem^,size);
  135.         {file copied, close file}
  136.         Close(tempFile);
  137.  
  138.         if (IOresult<>0) then begin
  139.             Writeln('ERROR CREATING BIG-FILE: Copy error with ',P,'.');
  140.             Dispose(dir);
  141.             FreeMem(mem,65535); Close(ScriptFile); Close(F);
  142.             exit;
  143.         end;
  144.  
  145.         inc(dir^.entries);
  146.         if (dir^.entries > MAXFILES) then begin
  147.             Writeln('ERROR CREATING BIG-FILE: Too many files.');
  148.             Dispose(dir);
  149.             FreeMem(mem,65535); Close(ScriptFile); Close(F);
  150.             exit;
  151.         end;
  152.     end;
  153.     FreeMem(mem,65535);
  154.     {close .lst file}
  155.     Close(ScriptFile);
  156.  
  157.     {get current fileposition = end of datafiles}
  158.     pos:=FilePos(F);
  159.     {write number of entries and position of directorybuffer}
  160.     Seek(F,0);
  161.     BlockWrite(F, dir^.entries, 2);
  162.     BlockWrite(F, pos, 4);
  163.  
  164.     {write directory buffer}
  165.     Seek(F, pos);
  166.     BlockWrite(F, dir^.entry[1], dir^.entries*SizeOf(FileRec));
  167.     Dispose(dir);
  168.  
  169.     {close our datafile}
  170.     Close(F);
  171.     if (IOresult<>0) then begin
  172.         Writeln('ERROR CREATING BIG-FILE: Couldn''t write directory block.');
  173.         exit;
  174.     end;
  175.  
  176.     CreateBigFile:=TRUE;
  177. {$I+}
  178. end;
  179.  
  180.  
  181. (*=--------------------------------------------=*)
  182.  
  183. function GetFile(name : string12; buffer : pointer) : boolean;
  184. var
  185.     i : word;
  186. begin
  187.     GetFile:=FALSE;
  188. {$I-}
  189.     i:=IOresult; {clear IOresult}
  190.     for i:=1 to Directory^.entries do
  191.         if (Directory^.entry[i].name=name) then with Directory^.entry[i] do begin
  192.             Seek(F,pos);
  193.             BlockRead(F, buffer^, size);
  194.             if IOresult<>0 then exit;
  195.             GetFile:=TRUE;
  196.             exit;
  197.         end;
  198. {$I+}
  199. end;
  200.  
  201. function GetFileSize(name : string12) : longint;
  202. var
  203.     i : word;
  204. begin
  205.     GetFileSize:=0;
  206.     for i:=1 to Directory^.entries do
  207.         if (Directory^.entry[i].name=name) then with Directory^.entry[i] do begin
  208.             GetFileSize:=size;
  209.             exit;
  210.         end;
  211. end;
  212.  
  213.  
  214. function InitFileSystem(name : string) : boolean;
  215. var
  216.     num : word;
  217.     pos : longint;
  218.     ok : boolean;
  219. begin
  220.     InitFileSystem:=FALSE;
  221. {$I-}
  222.     num:=IOresult; {clear IOresult}
  223.     {open big compiled file}
  224.     Assign(F, name);
  225.     Reset(F,1);
  226.     if (IOresult<>0) then exit; {not found}
  227.  
  228.     {read number of file entries and position of directorybuffer}
  229.     BlockRead(F, num, 2);
  230.     BlockRead(F, pos, 4);
  231.  
  232.     {read directory}
  233.     GetMem(Directory, 2+(num*SizeOf(FileRec)) );
  234.     Seek(F, pos);
  235.     Directory^.entries:=num;
  236.     BlockRead(F, Directory^.entry[1], num*SizeOf(FileRec) );
  237.     if (IOresult<>0) then exit;
  238. {$I+}
  239.     InitFileSystem:=TRUE;
  240. end;
  241.  
  242.  
  243. procedure CloseFileSystem;
  244. begin
  245.     FreeMem(Directory, 2 + (Directory^.entries * SizeOf(FileRec)) );
  246. {$I-}
  247.     Close(F);
  248. {$I+}
  249. end;
  250.  
  251. end.
  252.